/**
 * 份额流水
 * Created by KY on 2017/4/18.
 */
define(['ldfw/core', 'art', 'comps/ajax', 'comps/session','bscroll',
        'text!tpl/position/list.tpl',
        'text!tpl/position/row.tpl',
        'jquery-weui',
        'ldfw/helper/formatHelper'],
    function (LDFW, art, ajax, session,BScroll,tpl, rowTpl) {
        return LDFW.Container.extend({
            tpl: tpl,
            events: {
                'click .list-group .list-item': 'gotoDetail'
            },
            initialize: function () {
                if (is_custom_skin && is_custom_skin.trim()) {
                    this.model.set('is_custom_skin', is_custom_skin)
                    this.model.set('co_no', co_no)
                }
                this.model.set('show_profit',global_config.show_profit !== 0)
                this.once('shown', function () {
                    this.loadData()
                })
                this.loadData()
            },
            onCompAdd:function(comp){
                this.comp = comp
                this.comp.on('pullingDown',()=> {
                    this.loadData()
                })
            },

            loadData: function () {
                const _this = this
                //205:持仓列表; 204:累计盈亏
                $.when(ajax.post('pfL.1.205'), ajax.post('pfL.1.204'))
                    .done(function (resp1, resp2) {
                        if (!_.isEmpty(resp1[0].result)) {
                            let html = art.compile(rowTpl)({
                                list:resp1[0].result,
                                show_profit:global_config.show_profit !== 0
                            })
                            _this.$el.find('#positionList').html(html)
                        } else {
                            _this.$el.find('.posContent').html(
                                `<div id="blank-page">
                                    <img class="blank-page" src="/assets/img/blank.png" alt="无" display>
                                    <p>你还没有持仓哦 </p>
                                </div>`
                            )
                        }
                        if(resp2 && resp2[0]){
                            let accumulatedData = resp2[0].result
                            if(accumulatedData && accumulatedData.length){
                                accumulatedData = accumulatedData[0]
                                _this.model.set('total_asset',accumulatedData.total_asset/10000)
                                _this.model.set('realize_pandl',accumulatedData.realize_pandl/10000)
                            }
                        }
                        setTimeout(()=>{
                            _this.comp && _this.comp.forceUpdate()
                        },100)
                    })
            },
            gotoDetail: function (event) {
                let pd_no = $(event.currentTarget).attr('pd_no')
                LDFW.navigate('position/detail/' + pd_no)
            }
        })
    })